Main Page > Articles > Triangle Pattern > Coding Corrective Wave Patterns in MQL5: Zigzags, Flats, and Triangles

Coding Corrective Wave Patterns in MQL5: Zigzags, Flats, and Triangles

From TradingHabits, the trading encyclopedia · 5 min read · February 28, 2026
The Black Book of Day Trading Strategies
Free Book

The Black Book of Day Trading Strategies

1,000 complete strategies · 31 chapters · Full trade plans

Introduction

While impulse waves define the direction of the primary trend, corrective waves represent the equally important counter-trend movements. A thorough understanding and automated recognition of these corrective patterns are essential for any comprehensive Elliott Wave-based trading system. This article will provide a detailed guide to programming the three most common corrective patterns—Zigzags, Flats, and Triangles—within an MQL5 Expert Advisor. We will explore the unique characteristics of each pattern and translate them into actionable code.

The Structure of Corrective Waves

Corrective waves are typically composed of three main sub-waves, labeled A, B, and C. However, the internal structure of these waves varies depending on the type of correction.

  • Zigzag: A 5-3-5 pattern, where Wave A is a five-wave impulse, Wave B is a three-wave correction, and Wave C is a five-wave impulse. Zigzags represent a sharp correction against the main trend.
  • Flat: A 3-3-5 pattern, where Wave A is a three-wave correction, Wave B is a three-wave correction, and Wave C is a five-wave impulse. Flats generally move sideways.
  • Triangle: A 3-3-3-3-3 pattern, where all five waves (A, B, C, D, and E) are three-wave corrections. Triangles indicate a consolidation period before the final impulse wave.

Programming Logic for Zigzag Recognition in MQL5

To identify a Zigzag, we need to look for a three-wave structure with a 5-3-5 internal wave count. For simplicity, we will focus on the overall shape and Fibonacci relationships.

mql5
bool IsZigzag(double &wave_points[])
  {
   //--- A Zigzag is a 3-wave pattern (A, B, C)
   //--- For simplicity, we will use 3 swing points
   double waveA_length = wave_points[1] - wave_points[0];
   double waveB_length = wave_points[2] - wave_points[1];
   double waveC_length = wave_points[3] - wave_points[2];

   //--- Wave B should retrace a portion of Wave A (typically 38.2% to 78.6%)
   if (waveB_length / waveA_length < 0.382 || waveB_length / waveA_length > 0.786)
      return(false);

   //--- Wave C is often equal to or 1.618 times the length of Wave A
   if (waveC_length / waveA_length < 0.9 || waveC_length / waveA_length > 1.7)
      return(false);

   return(true);
  }

Programming Logic for Flat Recognition in MQL5

Flats are characterized by their sideways movement. Wave B typically retraces a significant portion of Wave A, and Wave C often terminates near the end of Wave A.

mql5
bool IsFlat(double &wave_points[])
  {
   double waveA_length = wave_points[1] - wave_points[0];
   double waveB_length = wave_points[2] - wave_points[1];
   double waveC_length = wave_points[3] - wave_points[2];

   //--- Wave B should retrace a large portion of Wave A (typically 90% to 138.2%)
   if (waveB_length / waveA_length < 0.9 || waveB_length / waveA_length > 1.382)
      return(false);

   //--- Wave C is often equal to or 1.618 times the length of Wave A
   if (waveC_length / waveA_length < 0.9 || waveC_length / waveA_length > 1.7)
      return(false);

   return(true);
  }

Programming Logic for Triangle Recognition in MQL5

Triangles are five-wave patterns (A, B, C, D, E) where each wave is a three-wave correction. The waves are bound by two converging trendlines.

mql5
bool IsTriangle(double &wave_points[])
  {
   //--- A Triangle is a 5-wave pattern (A, B, C, D, E)
   //--- This is a more complex pattern to identify and requires more advanced logic
   //--- For now, we will create a placeholder function
   return(false);
  }

Formula for Corrective Wave Target Projection

For a Zigzag correction, a common target for the end of Wave C is:

Wave C Target = End of Wave A - (Length of Wave A * 1.0)*

Data Table: Example Corrective Wave Ratios

PatternWave B Retracement of Wave AWave C Extension of Wave A
Zigzag38.2% - 78.6%100% - 161.8%
Flat90% - 138.2%100% - 161.8%
TriangleN/AN/A

Actionable Example: Trading a Flat Correction

If an EA identifies a Flat correction within an uptrend, it can be an opportunity to enter a long position near the end of Wave C. The stop-loss could be placed below the start of Wave A, and the profit target could be a new high above the previous impulse wave.

Conclusion

Automating the recognition of corrective wave patterns is a challenging but rewarding endeavor. By translating the specific rules and Fibonacci relationships of Zigzags, Flats, and Triangles into MQL5 code, we can build an Expert Advisor that has a more complete understanding of the market's structure. While this article has provided a foundational framework, further refinement and the inclusion of more complex variations are necessary for a truly robust trading system. The next articles in this series will continue to build upon this foundation, exploring the integration of other technical tools and advanced MQL5 programming techniques.